<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Artcate;
use app\admin\model\Article;
use app\util\File;
use app\admin\model\City;
use app\admin\model\Consultant;
use app\admin\model\Webcity;
use app\util\Util;
use Overtrue\Pinyin\Pinyin;
use think\Db;
use think\View;

/*
 * 分站城市
 */
class WebcityController extends CommonController
{
    /*
     * 城市列表
     */
    public function index()
    {
        $list = Webcity::order('add_time asc')->paginate(15);

        return view('index', [
            'list' => $list
        ]);
    }

    /*
     * 添加城市
     */
    public function add()
    {
        $provinceList = City::where('level', '=', City::LEVEL_1)->select();

        return view('add', [
            'provinceList' => $provinceList,
        ]);
    }

    /*
     * 获取城市
     */
    public function getCity()
    {
        $id = input('param.id');

        $cityList = City::where('pid', '=', $id)->field('id, name')->select();

        return Util::ajaxReturn(1, '查询成功', $cityList);
    }

    /*
     * 写入城市
     */
    public function postInsert()
    {
        $all = input('post.');

        $province_id = $all['province_id'];
        $city_id = $all['city_id'];
        $city = $all['city'];

        $exist = Webcity::where('city_id', '=', $city_id)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '城市已存在', null);
        }

        $pinyinObj = new Pinyin();
        $pinyinArray = $pinyinObj->convert($city);
        $pinyin = implode('', $pinyinArray);
        $firstChar = strtoupper(substr($pinyin, 0, 1));

        $pinyin = $firstChar . substr($pinyin, 1);

        $data = [
            'province_id' => $province_id,
            'city_id' => $city_id,
            'city' => $city,
            'pinyin' => $pinyin,
            'add_time' => time(),
        ];

        $result = Webcity::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑城市
     */
    public function edit()
    {
        $id = input('param.id');

        $provinceList = City::where('level', '=', City::LEVEL_1)->select();

        $row = Webcity::where('id', '=', $id)->find();
        return view('edit', [
            'provinceList' => $provinceList,
            'id' => $id,
            'row' => $row
        ]);
    }

    /*
     * 保存城市
     */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $province_id = $all['province_id'];
        $city_id = $all['city_id'];
        $city = $all['city'];

        $exist = Webcity::where('city_id', '=', $city_id)
            ->where('id', '<>', $id)
            ->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '城市已存在', null);
        }

        $pinyinObj = new Pinyin();
        $pinyinArray = $pinyinObj->convert($city);
        $pinyin = implode('', $pinyinArray);
        $firstChar = strtoupper(substr($pinyin, 0, 1));

        $pinyin = $firstChar . substr($pinyin, 1);

        $data = [
            'province_id' => $province_id,
            'city_id' => $city_id,
            'city' => $city,
            'pinyin' => $pinyin,
        ];

        $result = Webcity::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除城市
     */
    public function del()
    {
        $id = input('param.id');

        $result = Webcity::where('id', '=', $id)->delete();
        if ($result) {
            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }

    /*
     * 切换分站状态
     */
    public function toggleOpen()
    {
        $id = input('param.id');
        $is_open = input('param.is_open');

        $data = [
            'is_open' => $is_open,
        ];

        $result = Webcity::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '设置成功', null);
        } else {
            return Util::ajaxReturn(0, '设置失败', null);
        }
    }

    /*
     * 优秀顾问
     */
    public function consultant()
    {
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        $list = Consultant::with('webcity')
            ->where('webcity_id', '=', $webcity_id)
            ->order('add_time desc')
            ->paginate(15);

        return view('consultant', [
            'webcity' => $webcity,
            'list' => $list,
        ]);
    }

    /*
     * 添加顾问
     */
    public function addConsultant()
    {
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        return view('addconsultant', [
            'webcity' => $webcity
        ]);
    }

    /*
     * 写入顾问
     */
    public function postInsertConsultant()
    {
        $all = input('post.');

        $webcity_id = $all['webcity_id'];
        $name = $all['name'];
        $idcard = $all['idcard'];
        $idcard_expire = $all['idcard_expire'];
        $phone = $all['phone'];
        $job = $all['job'];
        $description = $all['description'];
        $intro = $all['intro'];
        $yewu = $all['yewu'];
        $star = $all['star'];
        $img = $all['img'];

        $exist = Consultant::where('webcity_id', '=', $webcity_id)
            ->where('name', '=', $name)
            ->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '该顾问已存在', null);
        }

        $data = [
            'webcity_id' => $webcity_id,
            'name' => $name,
            'idcard' => $idcard,
            'idcard_expire' =>strtotime($idcard_expire),
            'phone' => $phone,
            'job' => $job,
            'description' => $description,
            'intro' => $intro,
            'yewu' => $yewu,
            'star' => $star,
            'img' => $img,
            'add_time' => time(),
        ];

        $result = Consultant::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑顾问
     */
    public function editConsultant()
    {
        $id = input('param.id');
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        $row = Consultant::where('id', '=', $id)->find();
        return view('editconsultant', [
            'id' => $id,
            'webcity' => $webcity,
            'row' => $row
        ]);
    }

    /*
     * 保存顾问
     */
    public function postSaveConsultant()
    {
        $all = input('post.');

        $id = $all['id'];
        $webcity_id = $all['webcity_id'];
        $name = $all['name'];
        $idcard = $all['idcard'];
        $idcard_expire = $all['idcard_expire'];
        $phone = $all['phone'];
        $job = $all['job'];
        $description = $all['description'];
        $intro = $all['intro'];
        $yewu = $all['yewu'];
        $star = $all['star'];
        $img = $all['img'];

        $exist = Consultant::where('webcity_id', '=', $webcity_id)
            ->where('name', '=', $name)
            ->where('id', '<>', $id)
            ->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '该顾问已存在', null);
        }

        $data = [
            'webcity_id' => $webcity_id,
            'name' => $name,
            'idcard' => $idcard,
            'idcard_expire' =>strtotime($idcard_expire),
            'phone' => $phone,
            'job' => $job,
            'description' => $description,
            'intro' => $intro,
            'yewu' => $yewu,
            'star' => $star,
        ];

        $row = null;
        if ($img != '') {
            $row = Consultant::where('id', '=', $id)->field('img')->find();

            $data['img'] = $img;
        }

        $result = Consultant::where('id', '=', $id)->update($data);
        if ($result) {
            if ($img != '') {
                $file = new File();
                $file->deleteImage($row['img']);
            }

            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除顾问
     */
    public function delConsultant()
    {
        $id = input('param.id');

        $row = Consultant::where('id', '=', $id)->field('img')->find();

        $result = Consultant::where('id', '=', $id)->delete();
        if ($result) {
            $file = new File();
            $file->deleteImage($row['img']);

            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }

    /*
     * 贷款资讯
     */
    public function article()
    {
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        $list = Article::with('artcate')
            ->where('webcity_id', '=', $webcity_id)
            ->field('id, artcate_id, title, source, is_hot, add_time')
            ->order('add_time desc')
            ->paginate(15);

        return view('article', [
            'webcity' => $webcity,
            'list' => $list,
        ]);
    }

    /*
     * 添加贷款资讯
     */
    public function addArticle()
    {
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        return view('addarticle', [
            'webcity' => $webcity
        ]);
    }

    /*
     * 写入贷款资讯
     */
    public function postInsertArticle()
    {
        $all = input('post.');

        $webcity_id = $all['webcity_id'];
        $title = $all['title'];
        $source = $all['source'];
        $img = $all['img'];
        $content = $all['content'];

        $exist = Article::where('title', '=', $title)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '文章已存在', null);
        }

        Db::startTrans();

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();
        $artcateName = $webcity['city'] . '资讯';
        $artcateRow = Artcate::where('name', '=', $artcateName)->find();
        if (is_null($artcateRow)) {
            $artcateData = [
                'name' => $artcateName,
                'add_time' => time(),
            ];

            $insertId = Artcate::insertGetId($artcateData);
        } else {
            $insertId = $artcateRow['id'];
        }

        $data = [
            'webcity_id' => $webcity_id,
            'artcate_id' => $insertId,
            'title' => $title,
            'source' => $source,
            'img' => $img,
            'content' => $content,
            'add_time' => time(),
        ];
        $result = Article::insert($data);

        if ($insertId && $result) {
            Db::commit();

            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            Db::rollback();

            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
     * 编辑贷款资讯
     */
    public function editArticle()
    {
        $id = input('param.id');
        $webcity_id = input('param.webcity_id');

        $webcity = Webcity::where('id', '=', $webcity_id)->field('id, city')->find();

        $row = Article::where('id', '=', $id)->find();
        return view('editarticle', [
            'id' => $id,
            'webcity' => $webcity,
            'row' => $row
        ]);
    }

    /*
     * 保存贷款资讯
     */
    public function postSaveArticle()
    {
        $all = input('post.');

        $id = $all['id'];
        $title = $all['title'];
        $source = $all['source'];
        $img = $all['img'];
        $content = $all['content'];

        $exist = Article::where('title', '=', $title)
            ->where('id', '<>', $id)
            ->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '文章已存在', null);
        }

        $data = [
            'title' => $title,
            'source' => $source,
            'content' => $content,
        ];

        $row = null;
        if ($img != '') {
            $row = Article::where('id', '=', $id)->field('img')->find();

            $data['img'] = $img;
        }

        $result = Article::where('id', '=', $id)->update($data);
        if ($result) {
            if ($img != '') {
                $file = new File();
                $file->deleteImage($row['img']);
            }

            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
     * 删除贷款资讯
     */
    public function delArticle()
    {
        $id = input('param.id');

        $row = Article::where('id', '=', $id)->field('img')->find();

        $result = Article::where('id', '=', $id)->delete();
        if ($result) {
            $file = new File();
            $file->deleteImage($row['img']);

            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }

    /*
     * 修改贷款资讯热门
     */
    public function toggleHot()
    {
        $id = input('param.id');
        $is_hot = input('param.is_hot');

        $data = [
            'is_hot' => $is_hot,
        ];

        $result = Article::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '设置成功', null);
        } else {
            return Util::ajaxReturn(0, '设置失败', null);
        }
    }



}